package com.gtl.meishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.gtl.meishi.R;
import com.gtl.meishi.bean.MediaEntity;

import java.util.List;

/**
 * Created by zhangke on 16/7/9.
 */
public class GridViewAdapter extends BaseAdapter{
    private List <MediaEntity> beanList;
    private Context context;

    public GridViewAdapter(List<MediaEntity> beanList, Context context) {
        this.beanList = beanList;

        this.context = context;
    }

    @Override
    public int getCount() {
        return  beanList==null?0:beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return beanList==null?null:beanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;

        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.hot_list_item,viewGroup,false);
            holder=new Holder(view);
            view.setTag(holder);
        }else{
            holder= (Holder) view.getTag();
        }
        MediaEntity mediaEntity=beanList.get(i);

        if (mediaEntity.getMedia()!=null) {
            Picasso.with(context).load(mediaEntity.getMedia().getCover_pic()).into(holder.imageViewMedia);
            holder.mTextViewTitle.setText(mediaEntity.getRecommend_caption());
            holder.mTextViewNumber.setText(mediaEntity.getMedia().getLikes_count()+"");

        }else{
            Picasso.with(context).load(mediaEntity.getRecommend_cover_pic()).into(holder.imageViewMedia);
            holder.mTextViewTitle.setText(mediaEntity.getRecommend_caption());
            holder.mTextViewNumber.setText("");
        }



        return view;



    }


    class Holder{
        private  View view=null;
        ImageView imageViewMedia,imageViewUser;
        TextView mTextViewTitle,mTextViewNumber;
        public Holder(View view) {
            this.view = view;
            imageViewMedia= (ImageView) view.findViewById(R.id.imageview_media_hot_item);
            imageViewUser= (ImageView) view.findViewById(R.id.imageview_auther_hot_item);
            mTextViewTitle= (TextView) view.findViewById(R.id.textview_title_hot_item);
            mTextViewNumber= (TextView) view.findViewById(R.id.textview_number_hot_item);
        }
    }
}


